package com.woniumall.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniumall.pojo.Cart;
import com.woniumall.pojo.Order;
import com.woniumall.pojo.PayInfor;
import com.woniumall.pojo.User;
import com.woniumall.service.CartService;
import com.woniumall.service.OrderService;
import com.woniumall.service.impl.CartServiceImpl;
import com.woniumall.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther linke
 * @create 2021-11-03 10:25
 * 订单操作相关类
 *
 */
@WebServlet("/order/*")
public class OrderServlet extends  BaseServlet {
    private OrderService orderService = new OrderServiceImpl();
    private CartService cartService = new CartServiceImpl();

    /**
     * 获取页面传递的已选订单详情
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void creatOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取地址栏上的id值
        String jsonId = request.getParameter("id");
        String Json = URLDecoder.decode(jsonId, "utf-8");//json数据进行解码
        System.out.println(Json);
        //增加订单数据校验
        if(Json == null ){return; }
        //System.out.println(Json);
        //将json数据转换成int数组
        int[] ids = new ObjectMapper().readValue(Json.getBytes(), int[].class);
        //获取session中存储的值   页面操作可以更改数据库中的值
        //List<Cart> list = (List<Cart>)request.getSession().getAttribute("cartlist");//.getSession()

        //应该从购物车表中取值
        User user = (User)request.getSession().getAttribute("user");
        List<Cart> list = cartService.findAllByUid(user.getId());
        List<Cart> newMyCart = new ArrayList<>();
        for (int id : ids) {
            Cart cart = list.get(id);//遍历旧购物车清单
            if(cart == null){
                return;
            }
            newMyCart.add(cart);//将选中index的清单加入新购物车
        }
      /*  for (int id : ids) {
            list.remove(list.get(id));//将选中index的清单在旧购物车中删除
        }*/
        System.out.println(newMyCart);
        BigDecimal totalPrices = new BigDecimal("0");
        for (Cart cart : newMyCart) {
            BigDecimal nums = new BigDecimal(cart.getNums()+"");
            BigDecimal price = cart.getPrice();
            totalPrices = totalPrices.add(price.multiply(nums)) ;

        }
        //responseUtil(response,newMyCart);//将数据响应到订单确认页面
        request.setAttribute("totalPrices",totalPrices);
        request.getSession().setAttribute("newMyCart",newMyCart);//将新的购物车存储
        request.getSession().removeAttribute("cartlist");//将旧的购物车删除
        request.getRequestDispatcher("/jspPage/order.jsp").forward(request,response);
    }

    //创建订单服务
    public void createTrueOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String addressid = request.getParameter("addressid");//获取用户选择的地址id
        int addressId = Integer.parseInt(addressid);

        List<Cart> newMyCart = (List)request.getSession().getAttribute("newMyCart");//获取购物车清单列表
        User user = (User)request.getSession().getAttribute("user");//获取用户对象

        //创建订单
        PayInfor payInfor = orderService.createOrder(user,newMyCart,addressId);
        if(payInfor.getFlag()){//订单创建成功
            request.getSession().setAttribute("payInfor",payInfor);
            request.getSession().setAttribute("newMyCart",newMyCart);
            responseUtil(response,payInfor);
     /*       request.getRequestDispatcher("/jspPage/submit.jsp").forward(request,response);*/
            /*request.getSession().setAttribute("newMyCart",newMyCart);*/
        }else {
            System.out.println(payInfor.getMassage());
            request.getSession().setAttribute("newMyCart",newMyCart);
            responseUtil(response,payInfor);
        }
        /*if(payInfor.getMassage().contains("订单创建成功")){
            request.setAttribute("payInfor",payInfor);
        }*/
/*        request.setAttribute("newMyCart",newMyCart);
        request.getRequestDispatcher("/jspPage/submit.jsp").forward(request,response);*/
        //responseUtil(response,payInfor.getMassage());
       //Order order = orderService.createOrder();
        /*System.out.println(newMyCart);
        System.out.println(addressid);*/
    }

    //创建订单服务
    public void createTrueOrder2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String addressid = request.getParameter("addressid");//获取用户选择的地址id
        int addressId = Integer.parseInt(addressid);

        List<Cart> newMyCart = (List)request.getSession().getAttribute("newMyCart");//获取购物车清单列表
        User user = (User)request.getSession().getAttribute("user");//获取用户对象

        //创建订单
        PayInfor payInfor = orderService.createOrder(user,newMyCart,addressId);
        if(payInfor.getFlag()){//订单创建成功
            request.setAttribute("payInfor",payInfor);
            request.setAttribute("newMyCart",newMyCart);
            request.getRequestDispatcher("/jspPage/submit.jsp").forward(request,response);
            /*request.getSession().setAttribute("newMyCart",newMyCart);*/
        }else {
            System.out.println(payInfor.getMassage());
            request.getSession().setAttribute("newMyCart",newMyCart);
            responseUtil(response,payInfor);
        }
        /*if(payInfor.getMassage().contains("订单创建成功")){
            request.setAttribute("payInfor",payInfor);
        }*/
/*        request.setAttribute("newMyCart",newMyCart);
        request.getRequestDispatcher("/jspPage/submit.jsp").forward(request,response);*/
        //responseUtil(response,payInfor.getMassage());
        //Order order = orderService.createOrder();
        /*System.out.println(newMyCart);
        System.out.println(addressid);*/
    }
    //点击提交清除购物车session缓存
    public void submit(HttpServletRequest request, HttpServletResponse response) throws Exception {

        request.getSession().removeAttribute("cartlist");
        System.out.println("删除成功");
        response.sendRedirect(request.getContextPath());
    }

}
